/**
 * \file array_storage.hpp
 * 
 * \section COPYRIGHT
 *
 * Basis: A 3D Mathematics Library
 *
 * ---------------------------------------------------------------------
 *
 * Copyright (c) 2010, Don Olmstead
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *  3. Neither the name of organization nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BASIS_DETAIL_ARRAY_STORAGE_HPP_INCLUDED
#define BASIS_DETAIL_ARRAY_STORAGE_HPP_INCLUDED

namespace basis { namespace detail
{
	template <typename T>
	class array_storage
	{

		//------------------------------------------------------------
		// Member types
		//------------------------------------------------------------

		public:

			/// Value of type T
			typedef T value_type;
			/// Random access iterator
			typedef T* iterator;
			/// Constant random access iterator
			typedef const T* const_iterator;
			/// Reference to a type T
			typedef T& reference;
			/// Const reference to a type T
			typedef const T& const_reference;
			/// Unsigned integral type
			typedef std::size_t size_type;

		//------------------------------------------------------------
		// Construction/Destruction
		//------------------------------------------------------------

		public:

			/**
			 * Creates an instance of the array_storage class.
			 *
			 * \param size The number of elements to create.
			 */
			explicit array_storage(size_type size)
				: _values(new T[size])
				, _size(size)
			{ }

			/**
			 * Destroys an instance of the array_storage class.
			 */
			~array_storage()
			{
				delete[] _values;
			}

		//------------------------------------------------------------
		// Capacity
		//------------------------------------------------------------

		public:

			/**
			 * Retrieves the size of the valarray.
			 *
			 * \returns The size of the valarray.
			 */
			inline size_type size() const
			{
				return _size;
			}

		//------------------------------------------------------------
		// Element access
		//------------------------------------------------------------

		public:

			/**
			 * Array operator for the valarray class.
			 *
			 * \param i The index of the element.
			 * \returns The value of the element at the index.
			 */
			inline reference operator[] (size_type i)
			{
				return _values[i];
			}

			/**
			 * Array operator for the valarray class.
			 *
			 * \param i The index of the element.
			 * \returns The value of the element at the index.
			 */
			inline const_reference operator[] (size_type i) const
			{
				return _values[i];
			}

		private:

			/// The elements of the array
			T* _values;
			/// The size of the array
			size_type _size;

	} ; // end class array_storage

} } // end namespace basis::detail

#endif // end BASIS_DETAIL_ARRAY_STORAGE_HPP_INCLUDED
